package com.university.evaluation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.university.evaluation.entity.StudentScoreDetail;
import com.university.evaluation.mapper.StudentScoreDetailMapper;
import com.university.evaluation.service.StudentScoreDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class StudentScoreDetailServiceImpl extends ServiceImpl<StudentScoreDetailMapper, StudentScoreDetail> implements StudentScoreDetailService {

    @Autowired
    private StudentScoreDetailMapper studentScoreDetailMapper;

    @Override
    public List<StudentScoreDetail> findByStudentIdAndSystemId(Long studentId, Long systemId) {
        return studentScoreDetailMapper.findByStudentIdAndSystemId(studentId, systemId);
    }

    @Override
    public List<StudentScoreDetail> findBySystemIdAndStatus(Long systemId, Integer status) {
        return studentScoreDetailMapper.findBySystemIdAndStatus(systemId, status);
    }

    @Override
    public boolean submitScore(StudentScoreDetail scoreDetail) {
        scoreDetail.setStatus(0); // 0表示待审核
        scoreDetail.setCreateTime(LocalDateTime.now());
        return save(scoreDetail);
    }

    @Override
    public boolean auditScore(Long id, Integer status, String comment, Long auditorId) {
        StudentScoreDetail scoreDetail = getById(id);
        if (scoreDetail != null) {
            scoreDetail.setStatus(status);
            scoreDetail.setAuditComment(comment);
            scoreDetail.setAuditBy(auditorId);
            scoreDetail.setAuditTime(LocalDateTime.now());
            return updateById(scoreDetail);
        }
        return false;
    }

    @Override
    public boolean batchImport(List<StudentScoreDetail> scoreList) {
        LocalDateTime now = LocalDateTime.now();
        for (StudentScoreDetail score : scoreList) {
            score.setStatus(1); // 导入的成绩默认为已通过
            score.setAuditTime(now);
            if (score.getCreateTime() == null) {
                score.setCreateTime(now);
            }
        }
        return saveBatch(scoreList);
    }

    @Override
    public boolean updateScore(Long id, BigDecimal score, String evidenceUrl) {
        StudentScoreDetail scoreDetail = getById(id);
        if (scoreDetail != null) {
            scoreDetail.setScore(score);
            if (evidenceUrl != null) {
                scoreDetail.setEvidenceUrl(evidenceUrl);
            }
            scoreDetail.setStatus(0); // 修改后重新提交审核
            scoreDetail.setUpdateTime(LocalDateTime.now());
            return updateById(scoreDetail);
        }
        return false;
    }

    @Override
    public int countPendingAudit(Long systemId) {
        QueryWrapper<StudentScoreDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("system_id", systemId)
               .eq("status", 0); // 0表示待审核
return Math.toIntExact(count(wrapper));
    }

    @Override
    public List<StudentScoreDetail> findByItemId(Long itemId) {
        return studentScoreDetailMapper.findByItemId(itemId);
    }

    @Override
    public List<StudentScoreDetail> batchImport(MultipartFile file) {
        // TODO: 实现文件解析和批量导入逻辑
        // 这里只是返回空列表，保证编译通过
        return new ArrayList<>();
    }

}